﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Text.RegularExpressions;
using daStaticAnalyser.Analysers;
using daStaticAnalyser;

namespace DifferentAnalizers
{
    using daBase.Objects;
    using daBase.Analyser;
    [AnalyserDefaultSettings("Регулярный анализатор.", "", 0, "")]
    public class RegularAnalyser : Analyser
    {
        /// <summary>
        /// Набор регулярных выражений
        /// </summary>
        [Category("Особые")]
        [DisplayName("Шаблоны")]
        [Description("Регулярные выражения, которые будет использовать анализатор.")]
        public List<AnalyserRegularExpression> Regexes { get { return (settings as RegularSettings).Regexes; } set { (settings as RegularSettings).Regexes = value; Change(); } }

        /// <summary>
        /// Изменение свойств
        /// </summary>
        protected override void CheckChanges()
        {
            if ((settings as RegularSettings).Regexes.Count < 1) settings.Enabled = false;
        }

        /// <summary>
        /// Инициализация
        /// </summary>
        protected override void Init()
        {
            if (settings == null)
                settings = new RegularSettings();
            base.Init();
        }

        /// <summary>
        /// Собственно анализ
        /// </summary>
        /// <param name="sender">инициатор события</param>
        /// <param name="e">список токенов</param>
        public void Analyse(object sender, SourceArgs e)
        {
            if (settings.Enabled)
            {
                MatchCollection matches = Regex.Matches(e.Source, (settings as RegularSettings).Regex());
                foreach (Match match in matches)
                {
                    for (int i = 0; i < (settings as RegularSettings).Regexes.Count; i++)
                    {
                        if ((settings as RegularSettings).Regexes[i].IsCorrect && (match.Groups[string.Format("re{0}", i)].Captures.Count == 0))
                            AddError((settings as RegularSettings).Regexes[i].Name, (settings as RegularSettings).Regexes[i].Level, e.File.Name, 0, string.Empty, e.File.MetaTags, (settings as RegularSettings).Regexes[i].Hint);
                        else if (!(settings as RegularSettings).Regexes[i].IsCorrect)
                        {
                            foreach (Capture capture in match.Groups[string.Format("re{0}", i)].Captures)
                                AddError((settings as RegularSettings).Regexes[i].Name, (settings as RegularSettings).Regexes[i].Level, e.File.Name, e.File.Map.Line(capture.Index), capture.Value, e.File.MetaTags, (settings as RegularSettings).Regexes[i].Hint);
                        }
                    }
                }
                if (matches.Count == 0)
                {
                    foreach (AnalyserRegularExpression regex in (settings as RegularSettings).Regexes)
                    {
                        if (regex.IsCorrect)
                            AddError(regex.Name, regex.Level, e.File.Name, 0, string.Empty, e.File.MetaTags, regex.Hint);
                    }
                }
            }
        }
    }
}
